package concurrency;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  使用显示的Lock对象
 *
 * 2018年 10月 01日 星期一
 * @author fireway
 */
public class MutexEvenGenerator extends IntGenerator {
    private int mCurrentValue = 0;

    private Lock mLock = new ReentrantLock();

    @Override
    public int next() {
        mLock.lock();
        try {
            ++mCurrentValue;   // danger point here!
            Thread.yield();
            ++mCurrentValue;
            return mCurrentValue;
        } finally {
            mLock.unlock();
        }
    }

    public static void main(String[] args) {
        EvenChecker.test(new MutexEvenGenerator());
    }
}
